import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import '../../helper/fileHelper.dart';
import '../../spaces/filespace/fileSpace_add.dart';
import '../../spaces/filespace/fileSpace_helper.dart';
import '../../spaces/image_space/record_image_add%20copy.dart';

import '../../spaces/movie_space/record_image_add.dart';
import '../../spaces/movie_space/record_image_add2.dart';
import '../../spaces/report_space/report_add.dart';
import '../../spaces/report_space/report_add2.dart';
import '../../widgets/inputWidget.dart';
import '../../model/space_item.dart';
import '../../spaces/image_space/record_image_add.dart';
import '../../helper/routeHelper.dart';
import 'record_card.dart';

//import '../image_space/record_card.dart';

typedef RecordDeleteCallback = void Function(SpaceItem r1);
typedef RecordAddCallback = void Function(SpaceItem r1);

class RecordListPage extends StatefulWidget {
  @override
  RecordListPage(this.SpaceId, this.IfCanAdd, {this.AppBarTitle = ""});
  final String SpaceId;
  final bool IfCanAdd;
  final String AppBarTitle;
  @override
  RecordListPageState createState() => new RecordListPageState();
}

class RecordListPageState extends State<RecordListPage> {
  List<SpaceItem> RList = <SpaceItem>[];

  bool firstLoading = true;
  ScrollController _scrollController = new ScrollController();
  bool isEnd = false;

  @override
  initState() {
    super.initState();
    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        getMore();
      }
    });

    getNewer();
  }

  Future<Null> getMore() async {
    // await addOne();
    int count = 5;
    if (isEnd == true) return;
    int page = RList.length ~/ count;
    var list1 = await SpaceItem.GetList2(widget.SpaceId, page + 1, 5);
    var list2 = list1
        .where((p) => RList.any((x) => x.Guid == p.Guid) == false)
        .toList();

    setState(() {
      if (list1.length < count) isEnd = true;
      RList.insertAll(RList.length, list2);
    });
  }

  onDeleted(SpaceItem r1) => {
        setState(() {
          RList.remove(r1);
        })
      };

  onAdded(SpaceItem r1) => {
        setState(() {
          RList.insert(0, r1);
        })
      };

  onRefrushed() => getNewer();

  Future<Null> getNewer() async {
    var list1 = await SpaceItem.GetList2(widget.SpaceId, 1, 5);
    if (firstLoading == true) {
      setState(() {
        firstLoading = false;
      });
    }

    if (list1.length == 0) return;

    var list2 = list1
        .where((p) => RList.any((x) => x.Guid == p.Guid) == false)
        .toList();

    if (list2.length == 0) return;

    list2.insertAll(list2.length, RList);

    setState(() {
      RList = list2;
    });
  }

  buildListData(BuildContext context, int index) {
    var x = RList[index];
    if (x.FType == 2) return RecordCardHelper.MovCard(context, x, onDeleted);
    if (x.FType == 3) return RecordCardHelper.ReportCard(context, x, onDeleted);
    //  if (x.FType == 0) return  FileSpaceHelper.FileCard2(
    //                           context, RList[index], onDeleted);
    if (x.FType == 0) return RecordCardHelper.FileCard(context, x, onDeleted);
    return RecordCardHelper.ImgCard(context, x, onDeleted);
    //if (x.RType == 2) return RecordCard.AudioCard(context,x);
    //return new Container();
  }

  Widget getAddPanel() {
    List<Widget> list1 = <Widget>[];
    List<Widget> list2 = <Widget>[];
    List<Widget> list3 = <Widget>[];
    List<Widget> list4 = <Widget>[];
    List<Widget> list5 = <Widget>[];
    List<Widget> list6 = <Widget>[];

    //list1.add(InputWidget.getGreyInfo("水库实时数据", Helper.DateTimeFormat( sk.TM)));
    var v5 = TextButton(
        child: Text("拍照"),
        onPressed: () => RouteHelper.ShowPage(
            context, ImgRecordAdd2Page(widget.SpaceId, onAdded)));
    var v1 = TextButton(
        child: Text("文字"),
        onPressed: () => RouteHelper.ShowPage(
            context, ReportItemAddPage(widget.SpaceId, onAdded)));
    var v2 = TextButton(
        child: Text("相册"),
        onPressed: () => RouteHelper.ShowPage(
            context, ImgRecordAddPage(widget.SpaceId, onAdded)));
    var v3 = TextButton(
        child: Text("视频"),
        onPressed: () => RouteHelper.ShowPage(
            context, MovRecordAddPage(widget.SpaceId, onAdded, 2)));
    var v4 = TextButton(
        child: Text("文件"),
        onPressed: () => (FileSpaceHelper.fileOnUpdate(widget.SpaceId, "")));
    var v6 = TextButton(
        child: Text("录像"),
        onPressed: () => RouteHelper.ShowPage(
            context, MovRecordAdd2Page(widget.SpaceId, onAdded, 2)));

    list4.add(InputWidget.getShowRow([v5, v6], true));
    list4.add(InputWidget.getDivider());
    list4.add(InputWidget.getShowRow([v2, v3], true));
    list4.add(InputWidget.getDivider());
    list4.add(InputWidget.getShowRow([v1, v4], true));
    list1.add(InputWidget.inCard3(list4));
    list1.add(Container(
      height: 16.0,
      child: Text(""),
    ));
    return InputWidget.inCard2(list1);
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    return Scaffold(
      appBar: widget.AppBarTitle == ""
          ? null
          : AppBar(
              title: Text(
              widget.AppBarTitle,
            )),
      body: firstLoading == true
          ? Center(
              child: SpinKitCircle(
              color: Colors.blue,
            ))
          : new RefreshIndicator(
              child: ListView.builder(
                itemCount: RList.length + 1,
                itemBuilder: (context, index) {
                  if (index != RList.length) {
                    return buildListData(context, index);
                  } else {
                    if (isEnd == true) {
                      return Container(
                          margin: EdgeInsets.all(12.0),
                          child: Center(
                              child: Text(
                            "没有更多了",
                            style: TextStyle(color: Colors.grey),
                          )));
                    } else {
                      return new Container();
                    }
                  }
                },
                controller: _scrollController,
              ),
              onRefresh: getNewer,
            ),
      //floatingActionButton:widget.IfCanAdd? RecordCard.MenuButton(context, widget.SpaceId):null,
      //floatingActionButtonLocation: fabMenuLocation,
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {
          RouteHelper.showBottomPanel(context, getAddPanel());
        },
      ),
    );
  }
}
